/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.agentslab.Broker;

import aa.core.Actor;
import aa.core.ActorName;
import aa.core.CommunicationException;
import aa.core.Contract;
import aa.core.Platform;
import aa.tool.CyberOrgTuple;
import com.agentslab.constants.APIConstants;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mehadihasan
 */
public class GameBroker extends Actor {

    
    private final String regMethodname = "register";
    private final String regGDMMethodname = "register";
    private final String searchMethodName = "searchAll";
    private ActorName anCybDM = null;
    private ActorName anGameDM = null;
    private String showGameListMethodname="updateGameList";
    private final String statusMethod = "showStatus";


     public String getRegGDMMethodname() {
        return regGDMMethodname;
    }

    public String getSearchMethodName() {
        return searchMethodName;
    }

    

    
    /**
     *
     * @return
     */
    public String getRegMethodname() {
        return regMethodname;
    }

    
   

    /**
     * 
     */
    public GameBroker() {
    }

    /**
     * 
     * @param cyberOrgTuple
     * @param proposedContract
     */
   

     public void getGameList(Contract contract, ActorName sender, String startTime) {
         ArrayList arrayList = new ArrayList<String>();
         try {
            
            CyberOrgTuple[] tempList = (CyberOrgTuple[]) call(APIConstants.gdmActor, searchMethodName, contract);

            if (tempList == null || tempList.length==0){
                System.err.println("tuple list empty!");
            }
            for (CyberOrgTuple cyberOrgTuple : tempList) {
                if (cyberOrgTuple.getGameName()!=null && cyberOrgTuple.getGameName().compareTo("")!=0){
                    if (!arrayList.contains(cyberOrgTuple.getGameName())){
                        arrayList.add(cyberOrgTuple.getGameName());
                    }
                }
            }

            send(sender, showGameListMethodname, arrayList, startTime);
           
        } catch (CommunicationException ex) {
            ex.printStackTrace();
            send(sender, statusMethod, "Error occured while searching for games.");

        }
    }

    public Object[] getServerList(String gameName, Contract contract) {
         ArrayList arrayList = new ArrayList<String>();
         try {

            CyberOrgTuple[] tempList = (CyberOrgTuple[]) call(APIConstants.gdmActor, searchMethodName, contract);

            if (tempList == null || tempList.length==0){
                System.err.println("tuple list empty!");
            }
            for (CyberOrgTuple cyberOrgTuple : tempList) {
                if (cyberOrgTuple.getGameName()!=null && cyberOrgTuple.getGameName().compareTo("")!=0 && cyberOrgTuple.getGameName().compareTo(gameName)==0){
                    arrayList.add(cyberOrgTuple.getGameName()+" "+cyberOrgTuple.getDestination());

                }
            }
            return arrayList.toArray();

        } catch (CommunicationException ex) {
            ex.printStackTrace();
            return null;
        }
    }

}
